home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LTP_LevelGadget.c < prev    next >
C/C++ Source or Header  |  1996-10-09  |  6KB  |  304 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #ifdef DO_LEVEL_KIND
  15. VOID
  16. LTP_LevelGadgetDrawLabel(struct Gadget *Gadget,BOOL FullRefresh)
  17. {
  18.     LevelExtra *Special;
  19.     ObjectNode *Node;
  20.     LONG Current;
  21.  
  22.     Special = (LevelExtra *)Gadget->SpecialInfo;
  23.     Node = (ObjectNode *)Gadget->UserData;
  24.  
  25.     GetAttr(LVIA_Current,Special->LevelImage,(ULONG *)&Current);
  26.  
  27.     Current += Special->Plus;
  28.  
  29.     Node->Current = Current;
  30.  
  31.     if(Node->Label)
  32.     {
  33.         LayoutHandle *Handle = Special->Handle;
  34.  
  35.         if(FullRefresh)
  36.             LTP_DrawObjectLabel(Handle,Node);
  37.  
  38.         if(Special->LevelFormat)
  39.         {
  40.             UBYTE Buffer[256];
  41.             struct RastPort *RPort;
  42.             LONG LabelTop,LabelLeft;
  43.             LONG LevelWidth,LevelLen;
  44.  
  45.             RPort = &Special->Handle->RPort;
  46.  
  47.             switch(Special->LevelPlace)
  48.             {
  49.                 case PLACETEXT_LEFT:
  50.  
  51.                     LabelLeft    = Node->Left - INTERWIDTH;
  52.                     LabelTop    = Node->Top + (Node->Height - Handle->GlyphHeight) / 2;
  53.                     break;
  54.  
  55.                 case PLACETEXT_RIGHT:
  56.  
  57.                     LabelLeft    = Node->Left + Node->Width + INTERWIDTH + Special->MaxLevelWidth;
  58.                     LabelTop    = Node->Top + (Node->Height - Handle->GlyphHeight) / 2;
  59.                     break;
  60.  
  61.                 case PLACETEXT_ABOVE:
  62.  
  63.                     LabelLeft    = Node->Left + (Node->Width - Special->MaxLevelWidth) / 2 + Special->MaxLevelWidth;
  64.                     LabelTop    = Node->Top - (Handle->GlyphHeight + INTERHEIGHT);
  65.                     break;
  66.  
  67.                 case PLACETEXT_BELOW:
  68.  
  69.                     LabelLeft    = Node->Left + (Node->Width - Special->MaxLevelWidth) / 2 + Special->MaxLevelWidth;
  70.                     LabelTop    = Node->Top + Node->Height + INTERHEIGHT;
  71.                     break;
  72.             }
  73.  
  74.             LTP_SetPens(RPort,Handle->TextPen,Handle->BackgroundPen,JAM2);
  75.  
  76.             if(Special->DispFunc)
  77.                 Current = (*Special->DispFunc)(Gadget,Current);
  78.  
  79.             SPrintf(Buffer,Special->LevelFormat,Current);
  80.  
  81.             LevelLen = strlen(Buffer);
  82.  
  83.             LevelWidth = TextLength(RPort,Buffer,LevelLen);
  84.  
  85.             LTP_PrintText(RPort,Buffer,LevelLen,LabelLeft - LevelWidth,LabelTop);
  86.  
  87.             if(LevelWidth < Special->MaxLevelWidth)
  88.                 LTP_EraseBox(RPort,LabelLeft - Special->MaxLevelWidth,LabelTop,Special->MaxLevelWidth - LevelWidth,Handle->GlyphHeight);
  89.         }
  90.     }
  91. }
  92.  
  93.  
  94. /*****************************************************************************/
  95.  
  96.  
  97. ULONG
  98. LTP_LevelGadgetRender(struct Gadget *Gadget,struct gpRender *RenderMsg)
  99. {
  100.     LevelExtra *Special = (LevelExtra *)Gadget->SpecialInfo;
  101.  
  102.     DrawImageState(RenderMsg->gpr_RPort,Special->LevelImage,Gadget->LeftEdge,Gadget->TopEdge,Gadget->Flags & GFLG_DISABLED ? IDS_DISABLED : IDS_NORMAL,Special->Handle->DrawInfo);
  103.  
  104.     LTP_LevelGadgetDrawLabel(Gadget,RenderMsg->gpr_Redraw == GREDRAW_REDRAW);
  105.  
  106.     return(TRUE);
  107. }
  108.  
  109.  
  110. /*****************************************************************************/
  111.  
  112.  
  113. ULONG
  114. LTP_LevelGadgetGoActive(struct Gadget *Gadget,struct gpInput *InputMsg)
  115. {
  116.     ULONG Position,KnobWidth,Current,Max;
  117.     struct RastPort    *RPort;
  118.     LevelExtra *Special;
  119.     ULONG Result;
  120.     LONG State;
  121.     LONG Left;
  122.  
  123.     Special = Gadget->SpecialInfo;
  124.  
  125.         // Get the current state of affairs
  126.  
  127.     GetAttr(LVIA_Position,    Special->LevelImage,&Position);
  128.     GetAttr(LVIA_Current,    Special->LevelImage,&Current);
  129.     GetAttr(LVIA_KnobWidth, Special->LevelImage,&KnobWidth);
  130.     GetAttr(LVIA_Max,        Special->LevelImage,&Max);
  131.  
  132.     Left = InputMsg->gpi_Mouse.X;
  133.  
  134.     if(Left < Position)
  135.     {
  136.         if(Current > 0)
  137.         {
  138.             State = IDS_NORMAL;
  139.  
  140.             Current--;
  141.         }
  142.     }
  143.     else
  144.     {
  145.         if(Left > KnobWidth + Position + KnobWidth)
  146.         {
  147.             if(Current < Max)
  148.             {
  149.                 State = IDS_NORMAL;
  150.  
  151.                 Current++;
  152.             }
  153.         }
  154.         else
  155.         {
  156.             State = IDS_SELECTED;
  157.  
  158.             Special->MidOffset = KnobWidth + Position - Left;
  159.         }
  160.     }
  161.  
  162.     if(State == IDS_NORMAL)
  163.     {
  164.         SetAttrs(Special->LevelImage,
  165.             LVIA_Current,    Current,
  166.         TAG_DONE);
  167.  
  168.         LTP_LevelGadgetDrawLabel(Gadget,FALSE);
  169.  
  170.         *InputMsg->gpi_Termination = FALSE;
  171.  
  172.         Result = GMR_NOREUSE | GMR_VERIFY;
  173.     }
  174.     else
  175.         Result = GMR_MEACTIVE;
  176.  
  177.     if(RPort = ObtainGIRPort(InputMsg->gpi_GInfo))
  178.     {
  179.         DrawImageState(RPort,Special->LevelImage,Gadget->LeftEdge,Gadget->TopEdge,State,Special->Handle->DrawInfo);
  180.  
  181.         ReleaseGIRPort(RPort);
  182.     }
  183.  
  184.     return(Result);
  185. }
  186.  
  187.  
  188. /*****************************************************************************/
  189.  
  190.  
  191. ULONG
  192. LTP_LevelGadgetHandleInput(struct Gadget *Gadget,struct gpInput *InputMsg)
  193. {
  194.     if(InputMsg->gpi_IEvent->ie_Class == IECLASS_RAWMOUSE)
  195.     {
  196.         ULONG KnobWidth,Position,Current,Max,Result;
  197.         struct RastPort *RPort;
  198.         LevelExtra *Special;
  199.         LONG Left,Width;
  200.         BOOL DrawIt;
  201.  
  202.         Special = Gadget->SpecialInfo;
  203.  
  204.         GetAttr(LVIA_Position,    Special->LevelImage,&Position);
  205.         GetAttr(LVIA_KnobWidth, Special->LevelImage,&KnobWidth);
  206.         GetAttr(LVIA_Max,        Special->LevelImage,&Max);
  207.  
  208.         Left = InputMsg->gpi_Mouse.X + Special->MidOffset - KnobWidth;
  209.  
  210.         Width = Special->LevelImage->Width - 2 * KnobWidth;
  211.  
  212.         if(InputMsg->gpi_IEvent->ie_Code == SELECTUP)
  213.         {
  214.             *InputMsg->gpi_Termination = FALSE;
  215.  
  216.             Result = GMR_NOREUSE | GMR_VERIFY;
  217.         }
  218.         else
  219.             Result = 0;
  220.  
  221.         if(Left < 0)
  222.             Left = 0;
  223.         else
  224.         {
  225.             if(Left > Width)
  226.                 Left = Width;
  227.         }
  228.  
  229.         if(Max)
  230.         {
  231.             Current = (Left * Max + (Width / (Max * 2))) / Width;
  232.  
  233.             if(Current > Max)
  234.                 Current = Max;
  235.             else
  236.             {
  237.                 if(Current < 0)
  238.                     Current = 0;
  239.             }
  240.         }
  241.         else
  242.             Current = 0;
  243.  
  244.         SetAttrs(Special->LevelImage,
  245.             LVIA_Current,    Current,
  246.         TAG_DONE);
  247.  
  248.         if(Result || (Position != Left && Max))
  249.         {
  250.             DrawIt = TRUE;
  251.  
  252.             SetAttrs(Special->LevelImage,
  253.                 LVIA_Position,    Left,
  254.             TAG_DONE);
  255.         }
  256.         else
  257.             DrawIt = FALSE;
  258.  
  259.         if(DrawIt)
  260.         {
  261.             if(RPort = ObtainGIRPort(InputMsg->gpi_GInfo))
  262.             {
  263.                 DrawImageState(RPort,Special->LevelImage,Gadget->LeftEdge,Gadget->TopEdge,Result ? IDS_NORMAL : IDS_SELECTED,Special->Handle->DrawInfo);
  264.  
  265.                 ReleaseGIRPort(RPort);
  266.             }
  267.  
  268.             LTP_LevelGadgetDrawLabel(Gadget,FALSE);
  269.         }
  270.  
  271.         return(Result);
  272.     }
  273.     else
  274.         return(0);
  275. }
  276.  
  277.  
  278. /*****************************************************************************/
  279.  
  280.     // Note: this is not a class dispatcher, it's a custom gadget dispatcher
  281.  
  282. ULONG SAVE_DS ASM
  283. LTP_LevelGadgetDispatcher(REG(a0) struct Hook *Hook,REG(a2) struct Gadget *Gadget,REG(a1) Msg msg)
  284. {
  285.     switch(msg->MethodID)
  286.     {
  287.         case GM_HITTEST:
  288.             return(GMR_GADGETHIT);
  289.  
  290.         case GM_RENDER:
  291.             return(LTP_LevelGadgetRender(Gadget,(struct gpRender *)msg));
  292.  
  293.         case GM_GOACTIVE:
  294.             return(LTP_LevelGadgetGoActive(Gadget,(struct gpInput *)msg));
  295.  
  296.         case GM_HANDLEINPUT:
  297.             return(LTP_LevelGadgetHandleInput(Gadget,(struct gpInput *)msg));
  298.  
  299.         default:
  300.             return(0);
  301.     }
  302. }
  303. #endif /* DO_LEVEL_KIND */
  304.